package vip.xiaonuo.modular.listener;

import org.springframework.stereotype.Service;
import vip.xiaonuo.facedoor.modular.device.entity.Device;
import vip.xiaonuo.facedoor.modular.device.entity.DeviceCategory;
import vip.xiaonuo.facedoor.modular.device.service.DeviceService;
import vip.xiaonuo.facedoor.modular.device.service.DeviceSync;
import vip.xiaonuo.modular.yelevatordevice.entity.YElevatorDevice;
import vip.xiaonuo.modular.yelevatordevice.service.YElevatorDeviceService;

import javax.annotation.Resource;
import java.util.Optional;

@Service("StarisDeviceSyncService.class")
public class StarisDeviceSyncService implements DeviceSync<YElevatorDevice> {

    @Resource
    private YElevatorDeviceService elevatorDeviceService;

    @Resource
    private DeviceService deviceService;

    @Override
    public Long add(Object obj) {
        try {
            YElevatorDevice elevatorDevice = (YElevatorDevice) obj;
            Device device = new Device();
            this.createDevice(device, elevatorDevice);
            deviceService.saveOrUpdate(device);
            return device.getId();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    @Override
    public Long update(Object obj) {
        try {
            YElevatorDevice elevatorDevice = (YElevatorDevice) obj;
            if (elevatorDevice == null) {
                return 0L;
            }

            if (elevatorDevice.getBaseDeviceId() == null) {
                return this.add(obj);
            } else {
                Device device = deviceService.getById(elevatorDevice.getBaseDeviceId());
                if (device == null) {
                    return this.add(obj);
                } else {
                    this.createDevice(device, elevatorDevice);
                    deviceService.saveOrUpdate(device);
                    return 0L;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0L;
    }

    @Override
    public void delete(Object obj) {
        YElevatorDevice elevatorDevice = (YElevatorDevice) obj;
        if (elevatorDevice != null && elevatorDevice.getBaseDeviceId() != null) {
            Device device = deviceService.getById(elevatorDevice.getBaseDeviceId());
            if (device != null) {
                deviceService.removeById(device);
            }
        }
    }

    @Override
    public void saveRelate(Long id, YElevatorDevice deviceBase) {
        if (id != null && id > 0) {
            deviceBase.setBaseDeviceId(id);
            elevatorDeviceService.saveOrUpdate(deviceBase);
        }
    }

    private void createDevice(Device device, YElevatorDevice deviceBase) {
        device.setDeviceNo(deviceBase.getId().toString());
        device.setSnCode(deviceBase.getDeviceNo());
        device.setName(deviceBase.getName());
        device.setType(DeviceCategory.MAIN_TYPE_TRAIL);
        device.setSubType(DeviceCategory.SUB_TYPE_TRAIL);
        device.setInsLocation(Optional.ofNullable(deviceBase.getIp()).orElse("").concat(":").concat(Optional.ofNullable(deviceBase.getPort()).orElse("")));
        device.setStatus(1);
    }
}
